<?php 
	include("../../conn.php");
	mysql_query('SET NAMES UTF8'); 
	

	$result_data_array = array();

	$userid = $_REQUEST['userid'];
	$shopid = $_REQUEST['shopid'];
	$state = $_REQUEST['state'];

	if($state == null || $state == ''){
		$state = -1;
	}
	if(!$userid && !$shopid){
		$result_data_array['code'] = -100;
		$result_data_array['msg'] = '用户信息错误';
		echo json_encode($result_data_array);
		exit;
	}

	$page = $_REQUEST['page'];
	$pagesize = $_REQUEST['pagesize'];
	if(!$page){
		$page = 0;
	}
	if(!$pagesize){
		$pagesize = 10;
	}

	$order_stata_where = '';
	if($state != -1){
		$order_stata_where = ' and a.state = '.$state ;
	}

	$user_where = '';
	if($shopid){
		$user_where  = 'a.shopid = '.$shopid;
	}else if($userid){
		$user_where  = 'a.userid = '.$userid;
	}


	$orders_sql = "select "
	."a.ordersid,a.totalprice,a.return_volume_price,a.coupon_price,a.state,a.distribution_fee,a.shopid,a.kuaidi,a.numbers,a.freightRisk_price,a.buyFreightRisk,a.danhao,"
	."b.name,b.images as images,b.leimu"
	." from goodsorders as a left join shop as b on b.shopid = a.shopid"
	." where ".$user_where."  ".$order_stata_where . " AND a.is_delete = 1 "
	." order by a.ordersid desc limit ".$page*$pagesize.",$pagesize";
	$orders_query = mysql_query($orders_sql);

	$orders_data_array = array();


	while ($orders_row = mysql_fetch_assoc($orders_query)) {
		$orderid = $orders_row['ordersid'];
		$car_sql = "select "
		."a.goodsid,a.goodsname,a.fTitle,a.sTitle,a.price,a.number,"
		." b.yuanjia,b.images"
		." from car as a left join goods as b on b.goodsid = a.goodsid"
		." where a.ordersid = ".$orderid;
		$car_query = mysql_query($car_sql);

		$car_goods = array();
		$total_goods_num = 0;
		if($car_query){
			while ($car_row = mysql_fetch_assoc($car_query)) {
				$car_goods[] = $car_row;
				$total_goods_num+=$car_row['number'];
			}
			
		}

		// 查找快递公司
		$query=mysql_query("select * from express_delivery where id=".$orders_row['kuaidi']);
		$express_delivery=mysql_fetch_assoc($query);
		//查找快递公司 end
			


		//状态:0为待付款 1为待商家接单 2为待收货 3为待评价 4退款售后 5 交易成功 6交易关闭 7货到付款,8为商家接单，9为商家拒绝接单
		$order_state = $orders_row['state']+=0;

		//退货状态
		$orders_row['tuihuo_state'] = -1;
		$tuihuo_query = mysql_query("select * from tuihuo where ordersid=".$orderid);
		$tuihuo = mysql_fetch_assoc($tuihuo_query);
		if($tuihuo){
			$orders_row['tuihuo_state'] = $tuihuo['state'];
		}
		if($shopid){
			// 查找分类类别
			$query = mysql_query("select * from categroy_type where id=(select categroy_type_id from categroy where cateid='".$orders_row['leimu']."')");
			$categroy_type = mysql_fetch_assoc($query);
			$orders_row['categroy_type'] = $categroy_type;
		}
		$query = mysql_query("select * from tuihuo where ordersid=".$orderid);
		$tuihuo = mysql_fetch_assoc($query);
		$tuihuoid=0;
		if (is_array($tuihuo)) {
			$tuihuoid=$tuihuo[id];
		}
		$orders_row['order_total_price'] = round($orders_row['totalprice']*100 + $orders_row['distribution_fee']*100 - $orders_row['return_volume_price']*100 - $orders_row['coupon_price']*100)/100;
		if((int)$orders_row['buyFreightRisk'] == 1){
			$orders_row['order_total_price'] = round($orders_row['order_total_price']*100 + $orders_row['freightRisk_price']*100)/100;
		}
		$orders_row['express_delivery'] = $express_delivery;
		$orders_row['total_goods_num'] = $total_goods_num;
		$orders_row['tuihuoid'] = $tuihuoid;
		$orders_row['goods'] = $car_goods;
		$orders_data_array[] = $orders_row;
			
	}
	
	$result_data_array['code'] = 200;
	$result_data_array['msg'] = '获取成功';
	$result_data_array['data'] = $orders_data_array;
	echo json_encode($result_data_array);
 ?>